<?php
namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Traits\HasRoles;

/**
 * Class SchoolAdmin
 * @package App\Models
 * @author zjw
 */
class SchoolAdmin extends Authenticatable
{
    use Notifiable;
    use HasRoles;

    protected $guard_name = 'school';

    protected $hidden = [
        'access_token','expire_time'
    ];

    protected $fillable = [
        'phone','name','is_super','status','school_id'
    ];

    /**
     * @param $query
     * @param $schoolId
     * @return mixed
     */
    public function scopeSchoolIdIs($query, $schoolId)
    {
        return $query->when($schoolId, function($query) use ($schoolId) {
            $query->where("school_id", $schoolId);
        });
    }

    /**
     * @param $query
     * @param $phone
     * @return mixed
     */
    public function scopePhoneLike($query, $phone)
    {
        return $query->when($phone, function($query) use ($phone) {
            $query->where("phone", "like", $phone . "%");
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function school()
    {
        return $this->belongsTo(School::class);
    }

    /**
     * @param $val
     */
    public function setPasswordAttribute($val)
    {
        $this->attributes['password'] = Hash::make($val);
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeIsSchool($query)
    {
        return $query->where('school_id',\Auth::user()->school_id);
    }
}
